package com.rs.game.player.content.commands;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import com.rs.cache.loaders.NPCDefinitions;
import com.rs.cache.loaders.ObjectDefinitions;
import com.rs.game.Animation;
import com.rs.game.Graphics;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.ShopsHandler;
import com.rs.utils.Utils;

public class Debug {
	/*
	 * Handles Debug Commands
	 */
	public static boolean processCommand(final Player player, final String[] cmd, boolean console, boolean clientCommand) {
		if (clientCommand) {
			
		} else {
			switch (cmd[0]) {
			
			/*
			 * Open an render
			 * 
			 * Usage: ::render renderId
			 * Example: ::render 22
			 */
			case "render":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::render renderId");
					return true;
				}
				try {
					if (Integer.parseInt(cmd[1]) > Utils.getInterfaceDefinitionsSize()) {
						player.getPackets().sendGameMessage("Invalid renderId");
						return true;
					}
					if (Integer.parseInt(cmd[1]) == 956) {
						return true;
					}
					player.getAppearence().setRenderEmote(Integer.valueOf(cmd[1]));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::render renderId");
				}
				return true;
			/*
			 * Open an interface
			 * 
			 * Usage: ::interface interfaceId
			 * Example: ::interface 22
			 */
			case "interface":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::interface interfaceId");
					return true;
				}
				try {
					if (Integer.parseInt(cmd[1]) > Utils.getInterfaceDefinitionsSize()) {
						player.getPackets().sendGameMessage("Invalid InterfaceId");
						return true;
					}
					if (Integer.parseInt(cmd[1]) == 956) {
						return true;
					}
					player.getInterfaceManager().sendInterface(Integer.valueOf(cmd[1]));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::interface interfaceId");
				}
				return true;
				
			/*
			 * Open an interface with IComponentIDs
			 * 
			 * Usage: ::icomponent interfaceId
			 * Example: ::icomponent 22
			 */
			case "icomponent":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::icomponent interfaceId");
					return true;
				}
				try {
					if (Integer.parseInt(cmd[1]) > Utils.getInterfaceDefinitionsSize()) {
						player.getPackets().sendGameMessage("Invalid InterfaceId");
						return true;
					}
					if (Integer.parseInt(cmd[1]) == 956) {
						return true;
					}
					player.getInterfaceManager().sendInterface(Integer.parseInt(cmd[1]));
					for (int i = 0; i < Utils.getInterfaceDefinitionsComponentsSize(Integer.parseInt(cmd[1])); i++) {
						player.getPackets().sendIComponentText(Integer.parseInt(cmd[1]), i, "" + i + "");
					}
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::icomponent interfaceId");
					return true;
				}
				return true;
				
				/*
				 * Loop through the Renders
				 * 
				 * Usage: ::looprender
				 * Example: ::looprender
				 */
			case "looprender": 
				WorldTasksManager.schedule(new WorldTask() {
					int i = 580;
					@Override
					public void run() {
						if (player.hasFinished()) {
							stop();
						}
						player.getAppearence().setRenderEmote(i);
						player.getPackets().sendGameMessage("RenderId: <col=FF0000>" + i + "</col>.");
						i++;
 					}
				}, 0, 1);
				return true;
				
			/*
			 * Loop through the interfaces
			 * 
			 * Usage: ::loopinterface
			 * Example: ::loopinterface
			 */
			case "loopinterface":
				WorldTasksManager.schedule(new WorldTask() {
					int i = 1;
					
					@Override
					public void run() {
						if (player.hasFinished()) {
							stop();
						}
						player.getInterfaceManager().sendInterface(i);
						player.getPackets().sendGameMessage("InterfaceId: <col=FF0000>" + i + "</col>.");
						i++;
					}
				}, 0, 1);
				return true;
				
			/*
			 * See what a config does
			 * 
			 * Usage: ::config confidId value
			 * Example: ::config 1241 100
			 */
			case "config":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::config configId value");
					return true;
				}
				try {
					player.getPackets().sendGameMessage(cmd[1] + " " + cmd[2]);
					player.getVarsManager().sendVar(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::config configId value");
				}
				return true;
				
			/*
			 * Loop through configs
			 * 
			 * Usage: ::configloop
			 * Example: ::configloop
			 */
			case "configloop":
				WorldTasksManager.schedule(new WorldTask() {
					int i = 1425;
					
					@Override
					public void run() {
						if (player.hasFinished()) {
							stop();
						}
						try {
							player.getVarsManager().sendVar(i, 1);
							player.getPackets().sendGameMessage("Value: " + i);
							i++;
						} catch (NumberFormatException e) {
							return;
						}
						return;
					}
				}, 0, 1);
				return true;
				
			/*
			 * See what a global config does
			 * 
			 * Usage: ::gconfig confidId value
			 * Example: ::gconfig 780 100
			 */
			case "gconfig":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::gconfig configId value");
					return true;
				}
				try {
					player.getPackets().sendGlobalConfig(Integer.valueOf(cmd[1]), Integer.valueOf(cmd[2]));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::gconfig configId value");
				}
				return true;
				
			case "dumpnumbers":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::dumpnumbers nr1 nr2");
					return true;
				}
				String location1 = "data/numbers.txt";
				System.out.println("Starting dump at "+location1);
				for (int nr = Integer.valueOf(cmd[1]); nr < Integer.valueOf(cmd[2]); nr++) {
					try {
						BufferedWriter writer = new BufferedWriter(new FileWriter(location1, true));
						writer.write(nr+", ");
						writer.flush();
						writer.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			return true;	
			
			case "dumpobjects":
				String location = "data/items/ObjectList.txt";
				System.out.println("Starting dump at "+location);
				for (int ObjectId = 0; ObjectId < Utils.getObjectDefinitionsSize(); ObjectId++) {
					try {
						BufferedWriter writer = new BufferedWriter(new FileWriter(location, true));
						writer.write(ObjectDefinitions.getObjectDefinitions(ObjectId).getId()+" - " + 
								""+ObjectDefinitions.getObjectDefinitions(ObjectId).getName()+" - " +
								ObjectDefinitions.getObjectDefinitions(ObjectId).getFirstOption());
						writer.newLine();
						writer.flush();
						writer.close();
						if (ObjectDefinitions.getObjectDefinitions(ObjectId).getId() == Utils.getObjectDefinitionsSize()/2)
							System.out.println("50% of "+Utils.getObjectDefinitionsSize()+" Objects.");
						if (ObjectDefinitions.getObjectDefinitions(ObjectId).getId() == Utils.getObjectDefinitionsSize()-1)
							System.out.println("Finished "+Utils.getObjectDefinitionsSize()+" Objects.");
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			return true;	
				
			case "dumpnpcs":
				String location11 = "data/items/NPCList.txt";
				System.out.println("Starting dump at "+location11);
				for (int NPCId = 0; NPCId < Utils.getNPCDefinitionsSize(); NPCId++) {
					try {
						BufferedWriter writer = new BufferedWriter(new FileWriter(location11, true));
						writer.write(NPCDefinitions.getNPCDefinitions(NPCId).getId()+" - " + 
								""+NPCDefinitions.getNPCDefinitions(NPCId).getName()+" - " +
								NPCDefinitions.getNPCDefinitions(NPCId).hasOption("rewards"));
						writer.newLine();
						writer.flush();
						writer.close();
						if (NPCDefinitions.getNPCDefinitions(NPCId).getId() == Utils.getNPCDefinitionsSize()-1)
							System.out.println("Finished "+Utils.getNPCDefinitionsSize()+" NPCs.");
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			return true;	
			/*
			 * Loop through GlobalConfig
			 */
			case "gconfigloop":
				WorldTasksManager.schedule(new WorldTask() {
					int i = 0;
					
					@Override
					public void run() {
						if (player.hasFinished()) {
							stop();
						}
						try {
							player.getVarsManager().sendVar(i, 1);
							player.getPackets().sendGameMessage("Value: " + i);
							i++;
						} catch (Exception e) {
							return;
						}
						return;
					}
				}, 0, 1);
				return true;
				
			/*
			 * See what an animation is
			 * 
			 * Usage: ::anim animationId
			 * Example: ::anim 232
			 */
			case "anim":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::anim animationId");
					return true;
				}
				try {
					player.setNextAnimation(new Animation(Integer.valueOf(cmd[1])));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::anim animationId");
				}
				return true;
				
			/*
			 * Turn into an NPC
			 * 
			 * Usage: ::tonpc npcId
			 * Example: ::tonpc 21
			 */
			case "tonpc":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::tonpc npcId (-1 for player)");
					return true;
				}
				try {
					player.getAppearence().transformIntoNPC(Integer.valueOf(cmd[1]));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::tonpc npcId (-1 for player)");
				}
				return true;
				
			/*
			 * Spawn an NPC
			 * 
			 * Usage: ::npc npcId
			 * Example: ::npc 1
			 */
			case "npc":
				try {
					World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
					BufferedWriter bw = new BufferedWriter(new FileWriter("data/npcs/spawns.txt", true));
					bw.write("//" + NPCDefinitions.getNPCDefinitions(Integer.parseInt(cmd[1])).name + " spawned by " + player.getUsername());
					bw.newLine();
					bw.write(Integer.parseInt(cmd[1]) + " - " + player.getX() + " " + player.getY() + " " + player.getPlane());
					bw.flush();
					bw.newLine();
					bw.close();
				} catch (Throwable t) {
					t.printStackTrace();
				}
				return true;
				
			case "multinpc":
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				World.spawnNPC(Integer.parseInt(cmd[1]), player, -1, true, true);
				return true;

			/*
			 * Loop through all the interfaces
			 * 
			 * Usage: ::tryinter
			 * Example: ::tryinter
			 */
			case "tryinter":
				WorldTasksManager.schedule(new WorldTask() {
					int i = 549;
					
					@Override
					public void run() {
						if (i >= Utils.getInterfaceDefinitionsSize()) {
							stop();
							return;
						}
						if (player.hasFinished()) {
							stop();
						}
						player.getInterfaceManager().sendInterface(i);
						player.getPackets().sendGameMessage("Interface ID: <col=ff0000>" + i + "</col>");
						i++;
					}
				}, 0, 1);
				return true;
				
			/*
			 * Loop through all the graphics
			 * 
			 * Usage: ::trygfx
			 * Example: ::trygfx
			 */
			case "trygfx":
				WorldTasksManager.schedule(new WorldTask() {
					int i = 900;
					
					@Override
					public void run() {
						if (i >= Utils.getGraphicDefinitionsSize()) {
							stop();
						}
						if (player.hasFinished()) {
							stop();
						}
						player.setNextGraphics(new Graphics(i));
						player.getPackets().sendGameMessage("GFX ID: " + i);
						i++;
					}
				}, 0, 3);
				return true;
				
			/*
			 * Loop through all the looks
			 * 
			 * Usage: ::trylook lookId
			 * Example: ::trylook 23
			 */
			case "trylook":
				try {
					final int look = Integer.parseInt(cmd[1]);
					WorldTasksManager.schedule(new WorldTask() {
						int i = 269;
						
						@Override
						public void run() {
							if (player.hasFinished()) {
								stop();
							}
							player.getAppearence().setLook(look, i);
							player.getAppearence().generateAppearenceData();
							player.getPackets().sendGameMessage("Look ID: <col=ff0000>" + i + "</col>");
							i++;
						}
					}, 0, 1);
					return true;
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::trylook lookId");
				}
				
			/*
			 * Loop through all the animations
			 * 
			 * Usage: ::tryanim
			 * Example: ::tryanim
			 */
			case "tryanim":
				WorldTasksManager.schedule(new WorldTask() {
					int i = 15458;
					
					@Override
					public void run() {
						if (i >= Utils.getAnimationDefinitionsSize()) {
							stop();
							return;
						}
						if (player.getLastAnimationEnd() > Utils.currentTimeMillis()) {
							player.setNextAnimation(new Animation(-1));
						}
						if (player.hasFinished()) {
							stop();
						}
						player.setNextAnimation(new Animation(i));
						player.getPackets().sendGameMessage("Animation ID: <col=ff0000>" + i + "</col>");
						i++;
					}
				}, 0, 3);
				return true;
				
				
			/*
			 * Combine a GFX and Animation together
			 * 
			 * Usage: ::combine gfxId animId
			 * Example: ::combie 2032 15434
			 */
			case "combine":
				if (cmd.length < 3) {
					player.getPackets().sendGameMessage("Use: ::combine gfxId animId");
					return true;
				}
				try {
					player.setNextGraphics(new Graphics(Integer.valueOf(cmd[1])));
					player.setNextAnimation(new Animation(Integer.valueOf(cmd[2])));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::combine gfxId animId");
				}
				return true;
			/*
			 * Reset all your skills or just one
			 * 
			 * Usage: ::reset (optional:skillId)
			 * Example: ::reset (optional:21)
			 */
			case "reset":
				if (cmd.length < 2) {
					for (int i = 0; i < 25; i++) {
						if (i == Skills.HITPOINTS) {
							player.getSkills().setXp(i, 1184);
							player.getSkills().set(i, 10);
						} else {
							player.getSkills().setXp(i, 0);
							player.getSkills().set(i, 1);
						}
					}
				}
				try {
					if (Integer.valueOf(cmd[1]) == Skills.HITPOINTS) {
						player.getSkills().setXp(Integer.valueOf(cmd[1]), 1184);
						player.getSkills().set(Integer.valueOf(cmd[1]), 10);
					} else {
						player.getSkills().setXp(Integer.valueOf(cmd[1]), 0);
						player.getSkills().set(Integer.valueOf(cmd[1]), 1);
					}
					player.getAppearence().generateAppearenceData();
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::reset (optional:skillId)");
				}
				return true;
				
			/*
			 * Spawn an object
			 * 
			 * Usage: ::object objectId
			 * Example: ::object 23
			 */
			case "object":
				try {
					int type = cmd.length > 2 ? Integer.parseInt(cmd[2]) : 10;
					if (type > 22 || type < 0) {
						type = 10;
					}
					World.spawnObject(new WorldObject(Integer.valueOf(cmd[1]), type, 0, player.getX(), player.getY(), player.getPlane()));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::object objectId");
				}
				return true;
				
			/*
			 * See what a GFX does
			 * 
			 * Usage: ::gfx gfxId
			 * Example: ::gfx 34
			 */
			case "gfx":
				if (cmd.length < 2) {
					player.getPackets().sendGameMessage("Use: ::gfx gfxId");
					return true;
				}
				try {
					player.setNextGraphics(new Graphics(Integer.valueOf(cmd[1]), 0, 0));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::gfx gfxId");
				}
				return true;
				
			/*
			 * Clean home
			 */
			case "cleanhome":
			//	RemoveObjects.removeObjects();
				return true;
				
			/*
			 * Remove an object
			 * 
			 * Usage: ::removeobject xCoord yCoord
			 * Example: ::removeobject 2352 3552
			 */
		/*	case "removeobject":
				if (cmd.length < 3) {
					player.getPackets().sendGameMessage("Use: ::removeobject xCoord yCoord");
					return true;
				}
				try {
					int x = Integer.parseInt(cmd[1]);
					int y = Integer.parseInt(cmd[2]);
					
					WorldObject object = World.getObject(new WorldTile(x, y, player.getPlane()));
					if (object != null) {
						World.removeObject(object, true);
						player.getPackets().sendGameMessage("Removed object " + object.getId() + ".");
						return true;
					} else {
						player.getPackets().sendGameMessage("Invalid object. Please specify another coordinate.");
					}
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::removeobject xCoord yCoord");
				}
				return true;
				*/
			/*
			 * Master a skill or all skills
			 * 
			 * Usage: ::master (optional:skillId)
			 * Example: ::master (optional:24)
			 */
			case "master":
				if (cmd.length < 2) {
					for (int i = 0; i < 25; i++) {
						player.getSkills().addXp(i, Skills.MAXIMUM_EXP);
					}
				} else {
					try {
						player.getSkills().addXp(Integer.valueOf(cmd[1]), Skills.MAXIMUM_EXP);
					} catch (NumberFormatException e) {
						player.getPackets().sendGameMessage("Use: ::master (optional:skillId)");
					}
				}
				return true;
				
				
			/*
			 * Loop through all the objects
			 * 
			 * Usage: ::loopobject
			 * Example: ::loopobject
			 */
				
			case "loopobject":
				WorldTasksManager.schedule(new WorldTask() {
					//int i = 52728;
					int i = 44500;
					
					@Override
					public void run() {
						if (i >= Utils.getObjectDefinitionsSize()) {
							stop();
							return;
						}
						if (player.hasFinished()) {
							stop();
						}
						World.spawnObject(new WorldObject(i, 10, 0, player.getX(), player.getY(), player.getPlane()));
						player.getPackets().sendGameMessage("Object ID: <col=ff0000>" + i + "</col>");
						i++;
					}
				}, 0, 2);
				return true;
				
			/*
			 * Open up a shop
			 * 
			 * Usage: ::shop shopId
			 * Example: ::shop 14
			 */
			case "shop":
				try {
					ShopsHandler.openShop(player, Integer.valueOf(cmd[1]));
				} catch (NumberFormatException e) {
					player.getPackets().sendGameMessage("Use: ::shop shopId");
				}
				return true;
			}
		}
		return false;
	}
}